﻿Imports System.IO
Imports System.Net

Public Class gaysComm

    Private Function simpleHttpGetRequest(ByVal Url As String) As String
        Try
            Dim request As WebRequest = WebRequest.Create(Url)
            request.Method = "GET"
            Dim dataStream As Stream
            Dim response As WebResponse = request.GetResponse()
            dataStream = response.GetResponseStream()
            Dim reader As New StreamReader(dataStream)
            Dim responseFromServer As String = reader.ReadToEnd()
            reader.Close()
            dataStream.Close()
            response.Close()

            Return responseFromServer
        Catch ex As Exception
            ' TODO Logging
            MsgBox(ex.ToString)
            Return ""
        End Try
    End Function

    Private Function httpGetMessageCountSimple(ByVal Username As String, ByVal Password As String) As Integer
        Dim queryString As String = "http://www.gays2.de/api/messageCount/nickname/" + Username + "/password/" + Password
        Return CInt(simpleHttpGetRequest(queryString))
    End Function

    Public Function checkCredentials(ByVal Username As String, ByVal Password As String) As Boolean
        Return (httpGetMessageCountSimple(Username, Password) >= 0)
    End Function

    Public Function getMessageCount(ByVal Username As String, ByVal Password As String) As Integer
        Return httpGetMessageCountSimple(Username, Password)
    End Function

End Class
